﻿Ext.onReady(function () {
    Ext.Loader.setConfig({ enabled: true });
    Ext.Loader.setPath('Ext.ux', '../../js/ext/ux/statusbar');
    Ext.require([
    'Ext.ux.StatusBar'
]);
    Ext.BLANK_IMAGE_URL = '/css/Images/s.gif';
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var IsLogining = false;
    function Main_Login() {
        var logoPanel = new Ext.Panel({
            baseCls: 'x-plain',
            id: 'login-logo',
            heght: 132,
            region: 'center'
        });


        var checkcode = Ext.create('Ext.ux.CheckCode', {
            cls: 'key',
            fieldLabel: '验证码',
            name: 'CheckCode',
            id: 'CheckCode',
            allowBlank: false, labelAlign: 'right',
            isLoader: true,
            blankText: '验证码不能为空',
            codeUrl: '/manager/user/GetCheckCode',
            width: 260,
            enableKeyEvents: true,
            listeners: {
                keydown: function (field, e) {
                    if (e.getKey() == e.ENTER) {//当键盘按下enter键时触发事件
                        login();
                    }
                }
            }
        })

        var loginForm = Ext.create("Ext.form.Panel", {
            id: 'loginForm',
            region: 'south',
            baseCls: 'x-plain',
            border: false,
            bodyPadding: 10,
            buttonAlign: "center",
            fieldDefaults: {
                labelAlign: 'left',
                labelWidth: 160,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0', msgTarget: "side", labelAlign: 'right'
            },
            items: [{
                xtype: 'textfield',
                fieldLabel: '登录名',
                name: 'username',
                cls: 'username',
                blankText: '登录名不能为空',
                regex: /^[\da-zA-Z_]{1,20}$/,
                regexText: "请填写1～20个数字或字母",
                validateOnBlur: false,
                allowBlank: false,
                enableKeyEvents: true,
                listeners: {
                    keydown: function (field, e) {
                        if (e.getKey() == e.ENTER) {//当键盘按下enter键时触发事件
                            loginForm.getForm().findField("userpwd").focus();
                        }
                    }
                }
            }, {
                xtype: 'textfield',
                inputType: 'password',
                name: 'userpwd',
                cls: 'pwd',
                blankText: '密码不能为空',
                regex: /^[\da-zA-Z_]{1,20}$/,
                regexText: "请填写1～20个数字或字母",
                fieldLabel: '密码',
                validateOnBlur: false,
                allowBlank: false,
                enableKeyEvents: true,
                listeners: {
                    keydown: function (field, e) {
                        if (e.getKey() == e.ENTER) {//当键盘按下enter键时触发事件
                            loginForm.getForm().findField("CheckCode").focus();
                        }
                    }
                }
            }, checkcode
        ],
            buttons: [{
                text: '登录',
                cls: "x-btn-text-icon",
                icon: "/css/Images/icons/lock_open.png",
                height: 30,
                id: 'btnLogin',
                handler: function () {
                    loginForm.down('#btnLogin').setDisabled(true);
                    login();
                }
            }, {
                text: '重置',
                cls: "x-btn-text-icon",
                icon: "/css/Images/icons/arrow_redo.png",
                height: 30,
                handler: function () {
                    loginForm.form.reset();
                }

            }]
        });



        function login() {
            if (loginForm.form.isValid()) {
                if (!IsLogining) {
                    IsLogining = true;
                    loginForm.down('#btnLogin').setDisabled(true);
                    loginForm.form.submit({
                        url: '/manager/user/login',
                        success: function (form, action) {
                            var flag = action.result.success;
                            if (flag == "true") {
                                Ext.Msg.show({
                                    title: '提示',
                                    msg: '登录成功，正在跳转页面。。。',
                                    width: 300,
                                    icon: Ext.window.MessageBox.INFO
                                });
                                window.location.href = '/manager/home/index';
                            }
                            else {
                                this.disabled = false;
                                loginForm.down('#btnLogin').setDisabled(false);
                                IsLogining = false;
                                //console.log(action);
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("登录失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "登录失败");

                                }
                            }

                        },
                        // 特别注意对于form.submit提交触发failure的条件是返回的success=false,和ajax.request是不一样的
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", action.result.msg);
                            IsLogining = false;
                        }
                    });
                }
            }
        }


        var win = new Ext.Window({
            title: '登录窗口',
            id: "loginWidow",
            iconCls: 'locked',
            width: 514,
            height: 364,
            resizable: false,
            draggable: true,
            modal: false,
            closable: false,
            layout: 'border',
            bodyStyle: 'padding:5px;',
            plain: false,
            items: [logoPanel, loginForm],
            buttonAlign: 'center'
            ,
            bbar: Ext.create('Ext.ux.StatusBar', {
                defaultText: '建议使用Google、火狐浏览器运行本程序',
                id: 'status-id',
                statusAlign: 'top'
                //                items: [{
                //                    text: 'A Button'
                //                }, '-', 'Plain Text']
            })

        });

        win.show();
    };
    Ext.onReady(function () {
        if (top != self) {
            top.location.href = "/manager/user/index";
        }
        else {
            //top.location.href = "/manager/user/index";
        }
        Main_Login();

    });
});
